package com.huluobo.a2201mvvm_imdemo.addGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.huluobo.a2201mvvm_imdemo.R;
import com.huluobo.a2201mvvm_imdemo.adapter.AddGroupAdapter;
import com.huluobo.a2201mvvm_imdemo.adapter.FindJoinedGroupAdapter;
import com.huluobo.a2201mvvm_imdemo.chat.ChatActivity;
import com.huluobo.a2201mvvm_imdemo.databinding.ActivityAddGroupBinding;
import com.huluobo.a2201mvvm_imdemo.mvvm.BaseMVVMActivity;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMGroupInfo;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends BaseMVVMActivity<ActivityAddGroupBinding, AddGroupViewModel> {
    private AddGroupAdapter addGroupAdapter;
    private FindJoinedGroupAdapter findJoinedGroupAdapter;
    private List<EMGroupInfo> groupInfoList = new ArrayList<>();

    private List<EMGroup> groupList = new ArrayList<>();

    @Override
    protected void initData() {
        viewModel.createGroupSuccess.observe(this, ToastUtils::showLong);

        viewModel.createGroupFailed.observe(this, s -> {
            Log.i("TagA", "创建群势必:" + s);
        });

        viewModel.getGroupsSuccess.observe(this, list -> {
            groupInfoList.clear();
            groupInfoList.addAll(list);
            dataBinding.addGroupRv.setAdapter(addGroupAdapter);
        });

        viewModel.getGroupsFailed.observe(this, s -> {
            Log.i("TagA", "查找群失败:" + s);
        });

        viewModel.joinGroupSuccess.observe(this, ToastUtils::showLong);

        viewModel.joinGroupFaild.observe(this, s -> {
            if (s.equals("601")) {
                ToastUtils.showLong("已经加入过该群,不能重复加入");
            } else {
                ToastUtils.showLong(s);
            }
        });

        viewModel.findJoinedGroupSuccess.observe(this, list -> {
            groupList.clear();
            groupList.addAll(list);
            dataBinding.addGroupRv.setAdapter(findJoinedGroupAdapter);
        });

        viewModel.findJoinedGroupFailed.observe(this, s -> {
            Log.i("TagA", "发现已加入的群失败:" + s);
        });
    }

    @Override
    protected void initView() {
        dataBinding.addGroupHeader.headerTitle.setText("添加群组");
        dataBinding.addGroupHeader.headerBack.setVisibility(View.VISIBLE);
        dataBinding.addGroupHeader.headerBack.setOnClickListener(v -> {
            finish();
        });

        dataBinding.addGroupCreateBtn.setOnClickListener(v -> {
            EMGroupOptions option = new EMGroupOptions();
            option.maxUsers = 100;
            option.style = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
            viewModel.createGroup(
                    dataBinding.addGroupName.getText().toString(),
                    dataBinding.addGroupDesc.getText().toString(),
                    new String[]{EMClient.getInstance().getCurrentUser()},
                    "这是创建群",
                    option
            );
        });

        dataBinding.addGroupFindGroup.setOnClickListener(v -> {
            viewModel.getGroups(10, null);
        });

        //初始化rv
        dataBinding.addGroupRv.setLayoutManager(new LinearLayoutManager(this));
        addGroupAdapter = new AddGroupAdapter(groupInfoList);
        dataBinding.addGroupRv.setAdapter(addGroupAdapter);

        addGroupAdapter.setOnAddClick(groupId -> {
            viewModel.joinGroup(groupId);
        });

        findJoinedGroupAdapter = new FindJoinedGroupAdapter(groupList);
        dataBinding.addGroupFindJoinedGroup.setOnClickListener(v -> {
            viewModel.findJoinedGroup();
        });
        //当使用itemChildClick的时候,必须要先实现一下这个view的可点击
        findJoinedGroupAdapter.addChildClickViewIds(R.id.item_findJoinedGroup_toChat);
        findJoinedGroupAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.item_findJoinedGroup_toChat) {
                String conversationId = groupList.get(position).getGroupId();
                Intent intent = new Intent(this, ChatActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("conversationId", conversationId);
                //聊天的类型,单聊为1 群聊为2 聊天室为3
                bundle.putInt("chatType", 2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    @Override
    protected Class<AddGroupViewModel> getViewModel() {
        return AddGroupViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_group;
    }
}